package com.sitecsys.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.sitecsys.config.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LHB
 * <p>des</p>
 **/

public class JwtUtils {

    //properties
    private static final String SIGN="adefaceeasyMI89AYISLAJspsl=da23s56ds";

    public static String genereteToken(Map<String,String> mapinfo){
        Calendar instance = Calendar.getInstance();
        //有效期为24小时
        instance.add(Calendar.MINUTE,60*24*60);
        //map用户存储claim——声明的信息
        Map<String,Object> map=new HashMap<>();
        //生成token
        JWTCreator.Builder builder = JWT.create();
        //添加payload
        mapinfo.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        //指定令牌过期时间
        builder.withExpiresAt(instance.getTime());
        //指定算法-生成token
        String token = builder.sign(Algorithm.HMAC256(SIGN));
        return token;
    }

    /**
     * 验证token的合法性
     * @param token token令牌值
     * throw 抛出异常-表示验证失败
     */
    public synchronized static void decodeToken(String token){
        JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }

    /**
     * 获取token信息的方法
     * @param token token令牌值
     * @return DecodedJWT对象
     * throw 抛出异常-表示验证失败
     */
    public synchronized static DecodedJWT getTokenInfo(String token)throws Exception{
        try {
            DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
            return decodedJWT;
        } catch (TokenExpiredException e) {
            // 处理token过期逻辑，提示用户重新登录或调用刷新token的逻辑
            throw new Exception("Token has expired.", e);

        } catch (JWTVerificationException otherException) {
            // 处理其他JWT验证异常
            throw new Exception("Invalid JWT token.", otherException);
        }
    }

}
